package com.framework.seedkit.base;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by yisong on 16/6/14.
 */
public class BaseApplication extends MultiDexApplication {
    private static BaseApplication instance = null;
    public static String token = null;

    @Override
    public void onCreate() {
        super.onCreate();

        setInstance(this);
        initImageLoader(this);
    }

    public static synchronized BaseApplication getInstance() {
        if(null == instance){
            instance = new BaseApplication();
        }

        return instance;
    }

    private void setInstance(BaseApplication instance) {
        BaseApplication.instance = instance;
    }

    //图片工具类初始化
    public static void initImageLoader(Context context){
        if(!ImageLoader.getInstance().isInited()){
            ImageLoaderConfiguration config = null;
//            if(BuildConfig.DEBUG) {
//                config = new ImageLoaderConfiguration.Builder(context)
//                        .threadPriority(Thread.NORM_PRIORITY - 1)
//                        .tasksProcessingOrder(QueueProcessingType.LIFO)
//                        .memoryCacheSizePercentage(13)
//                        .diskCacheSize(500 * 1024 * 1024)
//                        .build();
//            } else {
//                config = new ImageLoaderConfiguration.Builder(context)
//                        .threadPriority(Thread.NORM_PRIORITY - 2)
//                        .diskCacheSize(500 * 1024 * 1024)
//                        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
//                        .tasksProcessingOrder(QueueProcessingType.LIFO).build();
//            }

            config = new ImageLoaderConfiguration.Builder(context)
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    .diskCacheSize(500 * 1024 * 1024)
                    .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                    .tasksProcessingOrder(QueueProcessingType.LIFO).build();

            ImageLoader.getInstance().init(config);
        }
    }
}